package com.luoxiang.socialandroid.activity

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.adapter.TradeUnionDataAdapter
import com.luoxiang.socialandroid.base.BaseActivity
import com.luoxiang.socialandroid.bean.NumberBean
import com.luoxiang.socialandroid.bean.ReportData
import com.luoxiang.socialandroid.bean.TradeUnionData
import com.luoxiang.socialandroid.bean.TradeUnionDataBean
import com.luoxiang.socialandroid.common.Constants.MmkvConst.KEY
import com.luoxiang.socialandroid.common.Constants.MmkvConst.TOKEN
import com.luoxiang.socialandroid.databinding.ActivityTradeUnionDataBinding
import com.luoxiang.socialandroid.utils.AESUtil
import com.luoxiang.socialandroid.utils.MmkvUtil
import com.luoxiang.socialandroid.utils.NetUtils
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

class TradeUnionDataActivity : BaseActivity() {
    private lateinit var mViewBinding: ActivityTradeUnionDataBinding
    private lateinit var adapter: TradeUnionDataAdapter
    private val data = mutableListOf<ReportData>()
    private var page = 1

    override fun initLayout(savedInstanceState: Bundle?) {
        mViewBinding = ActivityTradeUnionDataBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
        setStatusBarTranslucent()
    }

    override fun initView() {
        adapter = TradeUnionDataAdapter(data)
        mViewBinding.recyclerView.adapter = adapter
        mViewBinding.recyclerView.layoutManager = LinearLayoutManager(this)
        getData()
    }

    override fun initListener() {
        mViewBinding.imageBack.setOnClickListener {
            finish()
        }
        mViewBinding.refreshLayout.setOnRefreshListener {
            page = 1
            getData()
        }
        mViewBinding.refreshLayout.setOnLoadMoreListener {
            page++
            getData()
        }
    }

    private fun getData() {
        getRequest()
        NetUtils.getInstance().apiService.getReportList(
            EXTRA,
            MmkvUtil.decodeString(TOKEN),
            page,
            8
        )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<NumberBean?> {
                override fun onSubscribe(d: Disposable) {}

                override fun onNext(numberBean: NumberBean?) {
                    val data = numberBean?.data
                    var decryptStr: String? = null
                    try {
                        decryptStr = AESUtil.aesDecryptString(data, KEY)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    val (_, tradeUnionData, _) = Gson().fromJson(
                        decryptStr,
                        TradeUnionDataBean::class.java
                    ) ?: return
                    getDataSuccess(tradeUnionData)
                }

                override fun onError(e: Throwable) {}

                override fun onComplete() {}
            })
    }

    private fun getDataSuccess(tradeUnionData: TradeUnionData) {
        if (page == 1) {
            data.clear()
            mViewBinding.refreshLayout.finishRefresh()
        } else {
            mViewBinding.refreshLayout.finishLoadMore()
        }
        if (page >= (tradeUnionData.total ?: 0)) {
            mViewBinding.refreshLayout.setEnableLoadMore(false)
        } else {
            mViewBinding.refreshLayout.setEnableLoadMore(true)
        }
        tradeUnionData.reports?.let { data.addAll(it) }
        adapter.notifyDataSetChanged()
    }
}